﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GameSPK.Model;
using System.Windows.Controls;
using GameSPK.Helpers;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameSPK.DAO;
using System.Windows;

namespace GameSPK.Start
{
    class frmSection4Start: frmGenerel
    {
        private static frmSection4Start instance;
        private static object syncLock = new object();
        int goicauhoi;
        public static frmSection4Start getInstance(int goi)
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new frmSection4Start(goi);                     
                    }
                }
            }
            return instance;
        }
        private frmSection4Start(int goi)
            : base()
        {
            goicauhoi = goi;
            this.Closed += new EventHandler(frmSection4Start_Closed);
            this.Loaded += new System.Windows.RoutedEventHandler(frmSection4Start_Loaded);
            AddHandler(Keyboard.KeyDownEvent, (System.Windows.Input.KeyEventHandler)frmSection4Start_KeyDown);
            
        }

        void frmSection4Start_Closed(object sender, EventArgs e)
        {
            instance = null;
        }
        MultiEvent multiEvent;
        void frmSection4Start_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {

            //phanthichon = PhanThiDAO.getPhanThiChon(MainWindow.getCuocThiChon());
            filename = "section" + 4 + "childlayout.xml";
            
            timerClock = new System.Windows.Forms.Timer();
            timerClock.Tick += new EventHandler(timerClock_Tick);
            timerClock.Interval = 1000;
            cauhoi = CauHoiDAO.getCauHoiTrinhChieu(MainWindow.getCuocThiChon(), 4, 1);
            if (cauhoi == null)
                return;
            multiEvent = MultiEventHelp.getMultiEventFromXML(cauhoi.CauHoi1);
            timerClock.Enabled = false;
            Generate();

        }
        int thoigianchuyentiep;
        int sukien;
        void timerClock_Tick(object sender, EventArgs e)
        {
            timetick--;
            thoigianchuyentiep++;
            if (timetick > 0)
            {
                lblClock.Content = timetick;
                if (thoigianchuyentiep >= thoigiandoi)
                {
                    thoigianchuyentiep = 0;
                    sukien++;
                    if (multiEvent[sukien].eloaiDuKien == loaidukien.info)
                    {
                        lblContent.Visibility = System.Windows.Visibility.Visible;
                        tbcontent.Text = multiEvent[sukien].NoiDung.Trim();
                        mMediaEvent.Visibility = System.Windows.Visibility.Hidden;
                    }
                    else if (multiEvent[sukien].eloaiDuKien == loaidukien.media)
                    {
                        mMediaEvent.Visibility = System.Windows.Visibility.Visible;
                        mMediaEvent.Stop();
                        mMediaEvent.Source = new Uri(multiEvent[sukien].NoiDung, UriKind.RelativeOrAbsolute);
                        mMediaEvent.Play();
                        lblContent.Visibility = System.Windows.Visibility.Hidden;
                    }
                    
                }
            }
            else
            {
                lblClock.Content = timetick;
                lblContent.Visibility = System.Windows.Visibility.Hidden;
                lblAnswer.Visibility = System.Windows.Visibility.Visible;
                mMediaEvent.Visibility = System.Windows.Visibility.Hidden;
                timerClock.Enabled = false;
            }
        }
        string filename;
        LabelLayout lblLayoutClock;
        int timetick;
        Label lblClock;
        CauHoi cauhoi;
        Label lblAnswer;
        void Generate()
        {
            thoigianchuyentiep = 0;
            sukien = 0;
            Button btnHome;
            Button btnAnswer;
            Control cbtnHome, cbtnAnswer;
            btnHome = new Button();
            cbtnHome = (Control)btnHome;
            LabelLayout lableXML;
            lableXML = Helper.getButtonFromXML(filename, "HomeButton");
            AssignControl.assignControlLayout(ref cbtnHome, lableXML, "btnHome");
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getHomeButtonUrl(), UriKind.RelativeOrAbsolute));
            btnHome.Background = myBrush;
            btnHome.Click += new System.Windows.RoutedEventHandler(btnHome_Click);
            gridMain.Children.Add(btnHome);

            btnAnswer = new Button();
            cbtnAnswer = (Control)btnAnswer;

            lableXML = Helper.getButtonFromXML(filename, "ShowAnswerButton");
            AssignControl.assignControlLayout(ref cbtnAnswer, lableXML, "btnAnswer");
            ImageBrush myBrush2 = new ImageBrush();
            myBrush2.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getAnswerButtonUrl(), UriKind.RelativeOrAbsolute));
            btnAnswer.Background = myBrush2;
            btnAnswer.Click += new System.Windows.RoutedEventHandler(btnAnswer_Click);
            gridMain.Children.Add(btnAnswer);
            //phan noi dung
            //title
            Label lblTitle = new Label();
            Control clblTitle = (Control)lblTitle;
            lableXML = Helper.getLabelFromXML(filename, "Title");
            AssignControl.assignControlLayout(ref clblTitle, lableXML, "lblTitle");
            lblTitle.FontSize = lableXML.FontSize;
            lblTitle.FontWeight = System.Windows.FontWeights.Normal;
            lblTitle.Foreground = lableXML.Foreground;
            lblTitle.Background = lableXML.Background;
            TextBlock tblblTitle = new TextBlock();
            tblblTitle.TextWrapping = TextWrapping.Wrap;
            tblblTitle.FontSize = lableXML.FontSize;
            tblblTitle.Text = multiEvent.cauhoi.Trim();
            lblTitle.Content = tblblTitle;


            gridMain.Children.Add(lblTitle);
            //answer
            lblAnswer = new Label();
            Control clblAnswer = (Control)lblAnswer;
            lableXML = Helper.getLabelFromXML(filename, "Answer");
            AssignControl.assignControlLayout(ref clblAnswer, lableXML, "lblAnswer");
            lblAnswer.FontSize = lableXML.FontSize;
            lblAnswer.FontWeight = System.Windows.FontWeights.Bold;
            lblAnswer.Foreground = lableXML.Foreground;
            lblAnswer.Background = lableXML.Background;
            lblAnswer.Visibility = System.Windows.Visibility.Hidden;

            lblAnswer.Content = "";


            gridMain.Children.Add(lblAnswer);
            //content
            lblContent = new Label();
            Control clblContent = (Control)lblContent;
            lableXML = Helper.getLabelFromXML(filename, "Content");
            AssignControl.assignControlLayout(ref clblContent, lableXML, "lblContent");
            lblContent.FontSize = lableXML.FontSize;
            lblContent.FontWeight = System.Windows.FontWeights.Normal;
            lblContent.Foreground = lableXML.Foreground;
            lblContent.Background = lableXML.Background;
            tbcontent = new TextBlock();
            tbcontent.TextWrapping = TextWrapping.Wrap;
            tbcontent.FontSize = lableXML.FontSize;
            tbcontent.Text = cauhoi.CauHoi1;
            lblContent.Content = tbcontent;
            lblContent.Visibility = System.Windows.Visibility.Hidden;
            gridMain.Children.Add(lblContent);
            mMediaEvent = new MediaElement();
            mMediaEvent.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            mMediaEvent.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            mMediaEvent.Margin = new System.Windows.Thickness(lableXML.Left, lableXML.Top, 0, 0);
            mMediaEvent.Width = lableXML.Width;
            mMediaEvent.Height = lableXML.Height;
            mMediaEvent.Stretch = Stretch.Fill;
            mMediaEvent.Name = "mMediaEvent";
            mMediaEvent.LoadedBehavior = MediaState.Manual;
            ///mMediaEvent.Source = new Uri(nguoichoi.Video, UriKind.RelativeOrAbsolute);
            //mMediaEvent.Play();
            mMediaEvent.Visibility = System.Windows.Visibility.Hidden;
            gridMain.Children.Add(mMediaEvent);
            if (multiEvent[0].eloaiDuKien == loaidukien.info)
            {
                lblContent.Visibility = System.Windows.Visibility.Visible;
                tbcontent.Text = multiEvent[0].NoiDung.Trim();
            }
            else if (multiEvent[0].eloaiDuKien == loaidukien.media)
            {
                mMediaEvent.Visibility = System.Windows.Visibility.Visible;
                mMediaEvent.Source = new Uri(multiEvent[0].NoiDung, UriKind.RelativeOrAbsolute);
                mMediaEvent.Play();
            }
            //clock
            lblClock = new Label();
            Control clblClock = (Control)lblClock;
            lblLayoutClock = Helper.getClockFromXML(filename);
            AssignControl.assignControlLayout(ref clblClock, lblLayoutClock, "lblClock");
            lblClock.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            lblClock.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            lblClock.FontSize = lblLayoutClock.FontSize;
            lblClock.FontWeight = System.Windows.FontWeights.Bold;
            lblClock.Style = (Style)this.FindResource("PulseLabel");
            lblClock.Foreground = lblLayoutClock.Foreground;
            lblClock.Content = lblLayoutClock.iTime1;
            DrawingBrush myDrawingBrush = new DrawingBrush();
            GeometryDrawing myGeometryDrawing = new GeometryDrawing();
            myGeometryDrawing.Brush = lblLayoutClock.Background;

            myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
            GeometryGroup ellipses = new GeometryGroup();
            ellipses.Children.Add(new EllipseGeometry(new Point(25, 50), 50, 50));
            myGeometryDrawing.Geometry = ellipses;
            myDrawingBrush.Drawing = myGeometryDrawing;
            lblClock.Background = myDrawingBrush;
            //lblClock.MouseDoubleClick += new MouseButtonEventHandler(lblClock_MouseDoubleClick);
            timetick = lblLayoutClock.iTime1;
            //phan chi thoi diem doi su kien
            thoigiandoi = timetick / multiEvent.sodukien;
            timerClock.Enabled = true;
            timerClock.Start();
            gridMain.Children.Add(lblClock);
                


        }
        int thoigiandoi;
        Label lblContent;
        TextBlock tbcontent;
        MediaElement mMediaEvent;
        
        void btnAnswer_Click(object sender, RoutedEventArgs e)
        {
            lblAnswer.Content = multiEvent.dapan.Trim();
            timerClock.Enabled = false;
        }
        System.Windows.Forms.Timer timerClock;
        void btnHome_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Close();
        }
        void frmSection4Start_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }
    }
}
